package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.service.WmsLoadingService;

import javax.servlet.http.HttpSession;

import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsLoadingController
 * @Description: 装车单
 * @Author Yjs
 * @Date Wed Jan 31 15:19:42 GMT+08:00 2018
 */
@RestController
@RequestMapping("/wmsloading")
@Api(value = "/wmsloading", description = "发货 出库发运")
public class WmsLoadingController extends BaseController<WmsLoadingService, WmsLoading> {

    @PostMapping(value = {"list"})
    @ApiOperation(value = "查询出库发运信息", response = JSONObject.class, notes = "POST查询出库发运信息")
    public @ResponseBody
    JSONObject getWmsLoading(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        JSONObject json = getJSONObject();
        Page<WmsShipmentOrder> list = new Page<WmsShipmentOrder>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                entity.setCkid(ckid);
                entity.setSshzid(hz.get(0).getHzid());//货主编号
                list = baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建装车单
     * @param wmsLoading
     * @param session
     * @return
     */
    @PostMapping(value = "addLoading")
    @ApiOperation(value = "创建装车单信息", response = JSONObject.class, notes = "创建装车单信息")
    public @ResponseBody
    JSONObject addLoading(@RequestBody WmsLoading wmsLoading, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            String cjr = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                cjr = userinfo.getUser().getName();
                baseService.addLoading(wmsLoading, hzid, cjr, ckid);
            }
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "创建装车单信息成功", "", "");
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "创建装车单信息失败", e.getMessage(), "");
        } catch (RuntimeException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "创建装车单信息失败", e.getMessage(), "");
        } catch (Exception e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "创建装车单信息失败", e.getMessage(), "");
        }
        return json;
    }

    /**
     * 创建装车单
     *
     * @param wmsLoading
     * @param session
     * @return
     */
    @PostMapping(value = "assistAddLoading")
    @ApiOperation(value = "协助发货创建装车单信息", response = JSONObject.class, notes = "协助发货创建装车单信息")
    public @ResponseBody
    JSONObject assistAddLoading(@RequestBody WmsLoading wmsLoading, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            String cjr = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                cjr = userinfo.getUser().getName();
                baseService.assistAddLoading(wmsLoading, hzid, cjr, ckid);
            }
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "创建装车单信息成功", "", "");
        } catch (RuntimeException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "创建装车单信息失败", e.getMessage(), "");
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "创建装车单信息失败", e.getMessage(), "");
        } catch (Exception e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "创建装车单信息失败", e.getMessage(), "");
        }
        return json;
    }
    /**
     * 承运人模糊查询
     *
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value = "searchCyr")
    @ApiOperation(value = "承运人", response = JSONObject.class, notes = "notes = \"查询输入{\\\"cyrmc\\\":\\\"承运人模糊查询\\\"}\"")
    public @ResponseBody
    List<WmsLoading> searchCyr(@RequestBody Map<String, String> map, HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        List<WmsLoading> wmsLoadingList = new ArrayList<>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            wmsLoadingList = baseService.searchCyr(map.get("cyrmc"), hzid, ckid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return wmsLoadingList;
    }

    /**
     * 承运车辆模糊查询
     *
     * @param map
     * @param session
     * @return
     */
    @PostMapping(value = "searchCph")
    @ApiOperation(value = "承运车辆", response = JSONObject.class)
    public @ResponseBody
    List<WmsLoading> searchCph(@RequestBody Map<String, String> map, HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        List<WmsLoading> wmsLoadingList = new ArrayList<>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            wmsLoadingList = baseService.searchCph(map.get("cph"), map.get("cyrid"), hzid, ckid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return wmsLoadingList;
    }
    /**
     * 1.单据类型为"抵扣"的发运订单可以点击"抵扣审核"，审核后改变抵扣订单下抵扣行对应的借货订单的借货抵扣状态；
     * 2.借货单预期数量等于抵扣数量,借货抵扣状态为"全部抵扣"；
     * 3.借货单预期数量大于抵扣数量,借货抵扣状态为"部分抵扣"；
     *
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "deductible")
    @ApiOperation(value = "抵扣审核", response = JSONObject.class, notes = "抵扣审核")
    public @ResponseBody
    JSONObject deductible(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                String hzid = hz.get(0).getHzid();
                baseService.deductible(setAndModifyModel, hzid, ckid);
            }
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "抵扣审核成功", "", "");
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "抵扣审核失败", e.getMessage(), "");
        } catch (Exception e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "抵扣审核失败", e.getMessage(), "");
        }
        return json;
    }

    /**
     * 打印装车单查询
     *
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "printLoading")
    @ApiOperation(value = "打印装车单", response = JSONObject.class)
    public @ResponseBody
    JSONObject printLoading(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        JSONObject json = getJSONObject();
        List<WmsLoading> wmsLoadingList = new ArrayList<>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            wmsLoadingList = baseService.printLoading(setAndModifyModel.getIds(), hzid, ckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(wmsLoadingList).toString(), wmsLoadingList.size());
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "打印装车单失败", e.getMessage(), "");
        } catch (Exception e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "打印装车单失败", e.getMessage(), "");
        }
        return json;
    }

    /**
     * 协助发货打印装车单查询
     *
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "assistPrintLoading")
    @ApiOperation(value = "协助发货打印装车单", response = JSONObject.class)
    public @ResponseBody
    JSONObject assistPrintLoading(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        JSONObject json = getJSONObject();
        List<WmsLoading> wmsLoadingList = new ArrayList<>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            wmsLoadingList = baseService.assistPrintLoading(setAndModifyModel.getIds(), hzid, ckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(wmsLoadingList).toString(), wmsLoadingList.size());
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "打印装车单失败", e.getMessage(), "");
        } catch (Exception e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "打印装车单失败", e.getMessage(), "");
        }
        return json;
    }
}
